package com.blate.dogxiao.decoration;

import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Px;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @author Az
 * on 2020/10/20
 */
public class VerticalListSpaceDecoration extends RecyclerView.ItemDecoration {

    private final int mLeftOffset;
    private final int mRightOffset;
    private final int mSpaceOffset;
    private final int mFirstTopOffset;
    private final int mLastBottomOffset;

    public VerticalListSpaceDecoration(@Px int leftOffset, @Px int rightOffset, @Px int space) {
        this(leftOffset, rightOffset, space, 0, 0);
    }

    public VerticalListSpaceDecoration(@Px int leftOffset, @Px int rightOffset, @Px int space,
                                       @Px int firstTopOffset, @Px int lastBottomOffset) {
        this.mLeftOffset = leftOffset;
        this.mRightOffset = rightOffset;
        this.mSpaceOffset = space;
        this.mFirstTopOffset = firstTopOffset;
        this.mLastBottomOffset = lastBottomOffset;
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        outRect.right = mRightOffset;
        if (parent.getAdapter() != null && parent.getChildAdapterPosition(view) == 0) {
            outRect.top = mFirstTopOffset;
        } else {
            outRect.top = mSpaceOffset;
        }
        outRect.left = mLeftOffset;
        if (parent.getAdapter() != null && parent.getChildAdapterPosition(view) == parent.getAdapter().getItemCount() - 1) {
            outRect.bottom = mLastBottomOffset;
        } else {
            outRect.bottom = 0;
        }
    }

}
